package com.docker.jy.service.impl;

import java.util.List;
import com.docker.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.docker.jy.mapper.PointsManagementMapper;
import com.docker.jy.domain.PointsManagement;
import com.docker.jy.service.IPointsManagementService;

/**
 * 积分管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@Service
public class PointsManagementServiceImpl implements IPointsManagementService 
{
    @Autowired
    private PointsManagementMapper pointsManagementMapper;

    /**
     * 查询积分管理
     * 
     * @param id 积分管理主键
     * @return 积分管理
     */
    @Override
    public PointsManagement selectPointsManagementById(Long id)
    {
        return pointsManagementMapper.selectPointsManagementById(id);
    }

    /**
     * 查询积分管理列表
     * 
     * @param pointsManagement 积分管理
     * @return 积分管理
     */
    @Override
    public List<PointsManagement> selectPointsManagementList(PointsManagement pointsManagement)
    {
        return pointsManagementMapper.selectPointsManagementList(pointsManagement);
    }

    /**
     * 新增积分管理
     * 
     * @param pointsManagement 积分管理
     * @return 结果
     */
    @Override
    public int insertPointsManagement(PointsManagement pointsManagement)
    {
        pointsManagement.setCreateTime(DateUtils.getNowDate());
        return pointsManagementMapper.insertPointsManagement(pointsManagement);
    }

    /**
     * 修改积分管理
     * 
     * @param pointsManagement 积分管理
     * @return 结果
     */
    @Override
    public int updatePointsManagement(PointsManagement pointsManagement)
    {
        return pointsManagementMapper.updatePointsManagement(pointsManagement);
    }

    @Override
    public int updatePoints(Long id, Long points, boolean isIncrease) {
        PointsManagement pointsManagement = selectPointsManagementById(id);
        if (pointsManagement == null) {
            throw new RuntimeException("积分管理信息不存在");
        }
        if (!isIncrease) {
            if (pointsManagement.getPoints() < points) {
                throw new RuntimeException("扣除积分不能超过当前积分");
            }
            pointsManagement.setPoints(pointsManagement.getPoints() - points);
        } else {
            pointsManagement.setPoints(pointsManagement.getPoints() + points);
        }
        return pointsManagementMapper.updatePointsManagement(pointsManagement);
    }

    /**
     * 批量删除积分管理
     * 
     * @param ids 需要删除的积分管理主键
     * @return 结果
     */
    @Override
    public int deletePointsManagementByIds(Long[] ids)
    {
        return pointsManagementMapper.deletePointsManagementByIds(ids);
    }

    /**
     * 删除积分管理信息
     * 
     * @param id 积分管理主键
     * @return 结果
     */
    @Override
    public int deletePointsManagementById(Long id)
    {
        return pointsManagementMapper.deletePointsManagementById(id);
    }
}
